<?php

namespace App\Models\Rooms;

use App\Models\Goods\Cates;
use App\Models\Rooms\GuessinGoodsImages;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class GuessinGoods extends Model
{
    use SoftDeletes;
    protected $table = 'guessin_goods';
    protected $datas = ['deleted_at'];
    protected $fillable = [
        'room_id','room_goods_name','room_goods_price','room_goods_img','is_show','room_goods_golden','room_goods_content','is_hot','sort','goods_cate_id','buy_back_price','room_goods_rules'
    ];

    public function setRoomIdAttribute($value)
    {

        return $this->attributes['room_id'] = implode(',', $value);
    }

    public function getRoomIdAttribute($value)
    {
        if(is_array($value))
        {
            return $this->attributes['room_id'] = $value;
        }else{
            $data = explode(',', $value);
            foreach ($data as $key => $value) {
                $data[$key] = intval($value);
            }
            return $this->attributes['room_id'] = $data;
        }

    }

    public function setRoomGoodsGoldenAttribute($value)
    {
        $arr = [];
        foreach ($value as $key => $v) {
            if($key == 'level1')
            {
                $arr[$key]['id'] = 1;
                $arr[$key]['member'] = $v['member'];
                $arr[$key]['house_owner'] = $v['room'];
            }elseif($key == 'level2')
            {
                $arr[$key]['id'] = 2;
                $arr[$key]['member'] = $v['member'];
                $arr[$key]['house_owner'] = $v['room'];
            }elseif($key == 'level3'){
                $arr[$key]['id'] = 3;
                $arr[$key]['member'] = $v['member'];
                $arr[$key]['house_owner'] = $v['room'];
            }
        }
        return $this->attributes['room_goods_golden'] = json_encode(array_values($arr));
    }

    public function imgs()
    {
        return $this->hasMany(GuessinGoodsImages::class,'goods_id','id');
    }

    public function setGoodsCateIdAttribute($value)
    {
        return $this->attributes['goods_cate_id'] = $value[1] ?? 0;
    }

    public function getGoodsCateIdAttribute($value)
    {
        if(request()->route()->getName() == 'rooms_goods.index')
        {
            return $value;
        }
        $pid = Cates::find($value)->pid;
        return $this->attributes['goods_cate_id'] =  [$pid,intval($value)];
    }

    public function getRoomGoodsGoldenAttribute($vlaue){
        $data = json_decode($vlaue,true);
        foreach ($data as $key => $v) {
            if(!isset($v['id']))
            {
                continue;
            }
            if($v['id'] == 1)
            {
                $this->attributes['room_goods_golden_level1'] = $v;
            }else if($v['id'] == 2)
            {
                $this->attributes['room_goods_golden_level2'] = $v;
            }else if($v['id'] == 3)
            {
                 $this->attributes['room_goods_golden_level3'] = $v;
            }
        }
        return $this->attributes;
    }
}
